package me.jessyan.mvparms.demo.mvp.ui.activity.integral;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.jess.arms.base.BaseActivity;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.jess.arms.utils.DoubleClickUtils;
import com.jess.arms.widget.CustomPopupWindow;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import javax.inject.Inject;
import javax.inject.Named;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import me.jessyan.mvparms.demo.R;
import me.jessyan.mvparms.demo.di.component.integral.DaggerIntegralComponent;
import me.jessyan.mvparms.demo.di.module.integral.IntegralModule;
import me.jessyan.mvparms.demo.mvp.contract.integral.IntegralContract;
import me.jessyan.mvparms.demo.mvp.presenter.integral.IntegralPresenter;
import me.jessyan.mvparms.demo.mvp.ui.widget.ConcentraicCircles;
import me.jessyan.mvparms.demo.mvp.ui.widget.CustomIntColorChangeTextView;
import me.jessyan.mvparms.demo.mvp.ui.widget.CustomToolbar;
import me.jessyan.mvparms.demo.mvp.ui.widget.MonthDateView;
import me.jessyan.mvparms.demo.mvp.ui.widget.WeekDayView;
import me.jessyan.mvparms.demo.utils.DateUtils;
import me.jessyan.mvparms.demo.utils.PerfectClickListener;

import static com.jess.arms.utils.Preconditions.checkNotNull;

/**
 * 每日签到
 */
public class IntegralActivity extends BaseActivity<IntegralPresenter> implements IntegralContract.View ,CustomPopupWindow.CustomPopupWindowListener,PopupWindow.OnDismissListener {

    @BindView(R.id.week_v)
    WeekDayView weekV;
    @BindView(R.id.month_v)
    MonthDateView monthV;//月份按钮
    @BindView(R.id.toolbar)
    CustomToolbar toolbar;
    @BindView(R.id.sign_in_rules_tv)
    TextView signInRulesTv;
    @BindView(R.id.sign_in_v)
    ConcentraicCircles signInV;//点击签到按钮
    @BindView(R.id.score)
    TextView score;
    @BindView(R.id.gift_change_tv)
    TextView giftChangeTv;
    @BindView(R.id.ll)
    LinearLayout ll;
    @BindView(R.id.moth_tv)
    TextView mothTv;//中文月份（公历）
    @BindView(R.id.day_tv)
    TextView dayTv;//日期
    @BindView(R.id.week_tv)
    TextView weekTv;//星期几
    @BindView(R.id.tips_for_tv)
    CustomIntColorChangeTextView tipsForTv;//连续签到
    @BindView(R.id.tomorrow_scorce_tv)
    CustomIntColorChangeTextView tomorrowScorceTv;//明日
    @BindView(R.id.cumulative_sign_tv)
    CustomIntColorChangeTextView cumulativeSignTv;//累积签到
    @BindView(R.id.state_tv)
    TextView stateTv;
    @BindView(R.id.month_day_tv)
    TextView month_day_Tv;

    @Inject
    @Named("signInSuccessPop")
    CustomPopupWindow signInSuccessPop;
    @Inject
    @Named("signInRulesPop")
    CustomPopupWindow signInRulesPop;

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        DaggerIntegralComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .integralModule(new IntegralModule(this))
                .build()
                .inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_sign_in; //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {



        toolbar.setListener(new CustomToolbar.ToolbarListener() {
            @Override
            public void onLeftBack() {
                killMyself();
            }

            @Override
            public void onRightBack() {

            }
        });


        signInV.setStrs("点击签到","+30积分");
        monthV.setTextView(month_day_Tv,stateTv);
        Calendar c = Calendar.getInstance();

        mothTv.setText(DateUtils.getChineseMonth(c.get(Calendar.MONTH)));
        dayTv.setText(c.get(Calendar.DAY_OF_MONTH)+"");
        weekTv.setText(DateUtils.getWeek());

        tipsForTv.setTextIntColor("已连续签到 7 天");
        tomorrowScorceTv.setTextIntColor("签到奖励 50 积分 ");
        cumulativeSignTv.setTextIntColor("累计签到 14 天 ");

        signInSuccessPop.setOnDismissListener(this);
        signInRulesPop.setOnDismissListener(this);

        signInV.start();
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.snackbarText(message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }


    @OnClick({R.id.sign_in_rules_tv, R.id.sign_in_v, R.id.gift_change_tv,R.id.score_tv})
    public void onViewClicked(View view) {
        if (DoubleClickUtils.getInstance().isDouble(view)) {
            return;
        }

        switch (view.getId()) {
            //签到规则
            case R.id.sign_in_rules_tv:
                if(signInRulesPop!=null) {
                    signInRulesPop.show();
                    backgroundAlpha(0.7f);
                }
                break;
             // 签到
            case R.id.sign_in_v:
                signInV.setStrs("已签到","+50积分");

                List<Integer> list = new ArrayList<>();
                list.add(Calendar.getInstance().get(Calendar.DAY_OF_MONTH));
                monthV.setDaysHasThingList(list);
                tipsForTv.setTextIntColor("已连续签到 8 天");
                tomorrowScorceTv.setTextIntColor("签到奖励 50 积分 ");
                cumulativeSignTv.setTextIntColor("累计签到 15 天 ");
                if(signInSuccessPop!=null){
                    signInSuccessPop.show();
                    backgroundAlpha(0.7f);
                }
                signInV.stop();
                break;
             //积分
            case R.id.score_tv:
                startActivity(new Intent(IntegralActivity.this,MineScoreActivity.class));
                break;
             // 礼品兑换
            case R.id.gift_change_tv:
                startActivity(new Intent(this,GiftExchangeActivity.class));
                break;
        }
    }

    @Override
    public IntegralActivity getActivity() {
        return this;
    }

    @Override
    public void initPopupView(View contentView) {
            contentView.findViewById(R.id.sure_tv).setOnClickListener(new PerfectClickListener() {
                @Override
                protected void onNoDoubleClick(View v) {
                    if(signInSuccessPop!=null&&signInSuccessPop.isShowing()){
                        signInSuccessPop.dismiss();
                    }
                    if(signInRulesPop!=null&&signInRulesPop.isShowing()){
                        signInRulesPop.dismiss();
                    }
                }
            });
    }

    @Override
    public void onDismiss() {
        backgroundAlpha(1);
    }
}
